package com.andnnl.groupby;

import java.util.ArrayList;
import java.util.List;

/**
 * 每10个一批，少于3个，放最后一批
 * Created by chenss on 2020/1/3.
 */
public class ListPartitionTest {
    public static void main(String[] args) {
        test1(2);
        test1(5);
        test1(12);
        test1(13);
        test1(22);
    }
    private static void test1(int count) {
        List<String> list=new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list.add(""+(i+1));
        }
        int splitSize=10;
        int size = list.size();
        int times= size/splitSize+ (size%splitSize<3&&size>splitSize?0:1);//算总长
        List<List<String>> rs=new ArrayList<>();
        int idx=0;
        for (int i = 0; i < times; i++) {
            List<String> l=new ArrayList<>();
            int startIdx=0;//开始idx
            int fs=splitSize;//本次循环长度
            if(i==times-1){//处理最后一批
                startIdx=idx;
                fs=size;
            }
            for (int j = startIdx; j < fs; j++) {
                l.add(list.get(idx++));
            }
            rs.add(l);
        }
        System.out.println(rs);
    }
}
